# -*- coding:utf-8 -*-
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
class Solution:
    def __init__(self):
        self.res = ''


    def Serialize(self, root):
        # write code here
        # 前序遍历，返回字符串
        # if not root:
        #     self.res += '#'
        # self.res += str(root.val) + ','
        # if root.left:
        #     self.Serialize(root.left)
        # if root.right:
        #     self.Serialize(root.right)
        # return self.res
        res = []
        if not root:
            return '#'
        res.append(str(root.val))
        l = self.Serialize(root.left)
        res.append(l)

        r = self.Serialize(root.right)
        res.append(r)
        return ','.join(res)


    def Deserialize(self, s):
        # write code here
        s_list = s.split(',')
        root, _ = self.deserialize(s_list, 0)
        return root
    def deserialize(self, s, loc):
        if loc >= len(s) or s[loc] == '#':
            return None, loc + 1


        root = TreeNode(int(s[loc]))
        loc += 1
        root.left, loc= self.deserialize(s, loc)
        root.right, loc = self.deserialize(s, loc)
        return root, loc

tree = TreeNode(8)
tree.left = TreeNode(10)
tree.right = TreeNode(6)
tree.left.left = TreeNode(5)
tree.left.right = TreeNode(7)
tree.right.left = TreeNode(9)
tree.right.right = TreeNode(11)
test = Solution()
print(test.Serialize(tree))